/**
 * @file
 * A JavaScript file for the theme.
 *
 * In order for this JavaScript to be loaded on pages, see the instructions in
 * the README.txt next to this file.
 */

// JavaScript should be made compatible with libraries other than jQuery by
// wrapping it with an "anonymous closure". See:
// - http://drupal.org/node/1446420
// - http://www.adequatelygood.com/2010/3/JavaScript-Module-Pattern-In-Depth
(function ($, Drupal, window, document, undefined) {

$(document).ready(function(){
	$( "input.book-button, input.form-submit, a.btn-more" ).button();
	
});

$(document).ready(function(){
	$('#slider').anythingSlider({
		theme: "metallic",
		buildNavigation: false,
  		buildStartStop: false,
		expand: false, 
		autoPlay: true,
		hashTags: false,
		delay: 2000
	});
		
});

$(document).ready(function(){
	var basepath = Drupal.settings.basePath;
	
	$('.book-collect').click(function(){
		var dlg_book = $('#dlg');
		var nid = $(this).attr('nid');
		dlg_book.dialog({
		autoOpen: false,
		modal: true,
		title: '注意',
		height: 200,
		buttons: {
		'确定': function() {
			$.ajax({
				'url' : basepath + 'ajax_collect',
				'data' : {'nid': nid},
				'type' : 'GET',
				'success' : function() {
					dlg_book.dialog('close');
				}
			});
		}},
		});

		var html = '<p>确定要收藏这本书!</p>';

		dlg_book.dialog('open');
		dlg_book.html(html);
		delete dlg_book;
	});

	
	$('.book-s').click(function(){
		var dlgs = $('#dlgs');
		var dlg = $('#dlgs');
		var nids = $(this).attr('nid');
		$.ajax({
			'url' : basepath + 'ajax_user_status',
			'type' : 'GET',
			'data' : {'nid': nids},
			'success' : function(data) {
				if(data == 1) {
					dlgs.dialog({
						autoOpen: false,
						modal: true,
						title: '注意',
						height: 200,
						buttons: {
							'确定': function() {
								$.ajax({
									'url' : basepath + 'ajax_subscrib',
									'data' : {'nid': nids},
									'type' : 'GET',
									'success' : function() {
										dlgs.dialog('destroy');
									}
								});
							}
						},
					});
					var html = '<p>确定要订阅这本书！</p>';
					dlgs.dialog('open');
					dlgs.html(html);

				}
				if(data == 0) {
					dlg.dialog({
						autoOpen: false,
						modal: true,
						title: '注意',
						height: 200,
						buttons: {
							'确定': function() {
								dlg.dialog('destroy');
							}
						},
					});
					var html = '<p>您在借书架的书已经超过5本,如想继续借阅请先删除！</p>';
					dlg.dialog('open');
					dlg.html(html);
				}
				if(data == 2) {
					dlg.dialog({
						autoOpen: false,
						modal: true,
						title: '注意',
						height: 200,
						buttons: {
							'确定': function() {
								dlg.dialog('destroy');
							}
						},
					});
					var html = '<p>此书已经在您的借书架！</p>';
					dlg.dialog('open');
					dlg.html(html);
				}	
			}
		});

	});
});

$(document).ready(function(){
	var basepath = Drupal.settings.basePath;
	
	var dlg = $('#dlg-bookdelete');

	$('.book-delete').click(function(){
		var id = $(this).attr('id');
		var html = '<p>确定要删除这本书!</p>';

		dlg.dialog({
			autoOpen: false,
			modal: true,
			title: '注意',
			height: 200,
			buttons: {
				'确定': function() {
					$.ajax({
						'url' : basepath + 'ajax_book_delete',
						'data' : {'id': id},
						'type' : 'GET',
						'success' : function() {
							dlg.dialog('close');
							location.reload();
						}
					});
				}},
		});

		dlg.dialog('open');
		dlg.html(html);
	});
});


$(document).ready(function(){
	var _invisClass = 'invisible';
	var _animSpeed = 750;
	$('.gallery').each(function(){
		var _holder = $(this);
		var _slideEl = $('.slider', _holder);
		var _slides = $('.slide', _slideEl);
		var _btNext = $('.btn-next', _holder);
		var _btPrev = $('.btn-prev', _holder);
		var _active = 0;
		var _bigW, _minW;
		
		_btPrev.hide();
		_slides.each(function(_ind){
			var _slide = $(this);
			var _activeS = $('.active-slide', _slide);
			var _defaultS = $('.default-slide', _slide);
			if (_ind == _active) {
				_activeS.show().removeClass(_invisClass);
				_defaultS.hide().removeClass(_invisClass);
				_bigW = _activeS.width();
			}
			else {
				_activeS.hide().removeClass(_invisClass);
				_defaultS.show().removeClass(_invisClass);
				_minW = _defaultS.width();
			}
		})
		function checkButtons(){
			if (_active == 0) {
				_btPrev.hide();
				_btNext.show();
			}
			else if (_active == _slides.length - 1){
				_btPrev.show();
				_btNext.hide();
			}
			else {
				_btPrev.show();
				_btNext.show();
			}
		}
		
		function changeSlide(from, to){
			_active = to;
			_slideEl.animate({
				'margin-left':-_minW*_active
			}, _animSpeed);
			setTimeout(function(){
				_slides.eq(from).find('.active-slide').hide();
				_slides.eq(from).find('.default-slide').show();
				_slides.eq(to).find('.active-slide').fadeIn(_animSpeed/2);
				_slides.eq(to).find('.default-slide').fadeOut(_animSpeed/2);
			}, _animSpeed/2)
			checkButtons();
		}
		
		_btNext.click(function(){
			if (!_slideEl.is(':animated')) changeSlide(_active, _active+1)
			return false;
		})
		_btPrev.click(function(){
			if (!_slideEl.is(':animated')) changeSlide(_active, _active-1)
			return false;
		})
	})
});

// Place your code here.


})(jQuery, Drupal, this, this.document);
